//
//  LevelViewController.m
//  WayToHome
//
//  Created by A-01 on 2020/6/22.
//  Copyright © 2020 A-01. All rights reserved.
//

#import "LevelViewController.h"
#import "LevelCollectionViewCell.h"
#import "PlayViewController.h"

@interface LevelViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, weak)UILabel * titleLab;
@property (nonatomic, weak)UICollectionView * collectionView;

@end

@implementation LevelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.bottomImageView.hidden = YES;
    [self setupUI];
}
- (void)setupUI{
                
    UILabel * titleLab = [UILabel new];
            
    titleLab.text = @"Choose Level";
    titleLab.textColor = [UIColor colorWithHexString:@"0AB966"];
    titleLab.font = [UIFont boldSystemFontOfSize:24];
    titleLab.textAlignment = NSTextAlignmentCenter;
                
    self.titleLab = titleLab;
                
    [self.view addSubview:self.titleLab];
    
    
    //1.初始化layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
    layout.itemSize =CGSizeMake(87, 48);
    layout.minimumLineSpacing = 15;
    layout.minimumInteritemSpacing = 15;
        //设置collectionView滚动方向
    //    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        //2.初始化collectionView
    UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake((kScreenW - 495) / 2, 70 , 495, self.view.height - 75) collectionViewLayout:layout];
        
    collectionView.backgroundColor = [UIColor clearColor];
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.showsVerticalScrollIndicator = NO;
        //3.注册collectionViewCell
    //注意，此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
    [collectionView registerClass:[LevelCollectionViewCell class] forCellWithReuseIdentifier:@"LevelCollectionViewCell"];
        //4.设置代理
    collectionView.delegate = self;
    collectionView.dataSource = self;
    
    self.collectionView = collectionView;
    
    [self.view addSubview:self.collectionView];
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSArray * arr;
    NSInteger home = 0;
    switch (indexPath.item) {
        case 0:
            arr = @[@3,@13,@23,@33,@34];
            home = 44;
            break;
        case 1:
            arr = @[@4,@14,@24,@23,@33];
            home = 43;
            break;
        case 2:
            arr = @[@9,@19,@29,@39,@38];
            home = 48;
            break;
        case 3:
            arr = @[@5,@15,@14,@24,@34];
            home = 44;
            break;
        case 4:
            arr = @[@6,@16,@26,@25,@24,@34];
            home = 44;
            break;
        case 5:
            arr = @[@6,@16,@26,@25,@24,@34,@33];
            home = 43;
            break;
        case 6:
            arr = @[@3,@13,@14,@15,@16,@26,@36];
            home = 46;
            break;
        case 7:
            arr = @[@6,@16,@26,@27,@28,@29,@39];
            home = 49;
            break;
        case 8:
            arr = @[@4,@14,@15,@16,@26,@36,@35,@34];
            home = 44;
            break;
        case 9:
            arr = @[@0,@10,@20,@21,@22,@23,@24,@34];
            home = 44;
            break;
        case 10:
            arr = @[@4,@14,@15,@16,@26,@36,@35,@34,@33];
            home = 43;
            break;
        case 11:
            arr = @[@9,@19,@29,@28,@27,@26,@25,@35];
            home = 45;
            break;
        case 12:
            arr = @[@1,@11,@12,@22,@23,@33,@34];
            home = 44;
            break;
        case 13:
            arr = @[@5,@15,@16,@26,@36,@35,@34,@33];
            home = 43;
            break;
        case 14:
            arr = @[@1,@11,@12,@13,@23,@24,@25,@26,@36,@37];
            home = 47;
            break;
        case 15:
            arr = @[@7,@17,@18,@28,@38,@37,@36,@35,@34,@33];
            home = 43;
            break;
        case 16:
            arr = @[@8,@18,@28,@38,@37,@36,@35,@34,@33,@32];
            home = 42;
            break;
        case 17:
            arr = @[@8,@18,@17,@16,@15,@14,@13,@23,@33,@34,@35,@36];
            home = 46;
            break;
        case 18:
            arr = @[@3,@13,@12,@11,@21,@31,@32,@33,@34,@35,@36,@37];
            home = 47;
            break;
        case 19:
            arr = @[@6,@16,@15,@14,@13,@12,@22,@32,@33,@34,@35,@36,@37];
            home = 47;
            break;
        default:
            break;
    }
  
    PlayViewController * playVC = [PlayViewController new];
    playVC.level = [NSString stringWithFormat:@"%ld",(long)indexPath.row + 1];
    playVC.resultArray = arr;
    playVC.homeIndex = home;
    [self.navigationController pushViewController:playVC animated:YES];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return 20;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    LevelCollectionViewCell *cell = (LevelCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"LevelCollectionViewCell" forIndexPath:indexPath];
    //设置数据
    cell.titleLab.text = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
    NSString *img;
    if (indexPath.item % 2==0) {
        
        img = @"flower";
    }else{
        
        img = @"leaf";
    }
    cell.iconImageView.image = [UIImage imageNamed:img];
    return cell;
}


- (void)viewDidLayoutSubviews{

    [super viewDidLayoutSubviews];

    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(15);
    }];
}

@end
